package com.ningmengs789.live.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.ningmengs789.live.R;
import com.ningmengs789.live.http.OKHttpUtils;
import com.ningmengs789.live.Interface.RequestCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;


public class HtmlWebViewActivity extends BaseActivity {

    @BindView(R.id.tv_title_center)
    TextView tv_title_center;
    @BindView(R.id.myWeb)
    WebView myWeb;
    private int type = 0;//0 1

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_htmlweb);
        ButterKnife.bind(this);
        setHeadLayout();
        initAction();
        initData();
    }


    private void initAction() {
        tv_title_center.setVisibility(View.VISIBLE);
        type = getIntent().getIntExtra("type", 0);
        if (type == 0){
            tv_title_center.setText(getString(R.string.wszl_xieyi));
        }else {
            tv_title_center.setText(getString(R.string.yingsi));
        }
        WebSettings webSettings = myWeb.getSettings();
        webSettings.setJavaScriptEnabled(true);
    }

    private void initData() {
        if (type == 0){
            OKHttpUtils.getInstance().getRequest("app/user/getUserProtocol", "", new RequestCallback() {
                @Override
                public void onError(int errCode, String errInfo) {
                }
                @Override
                public void onSuccess(String result) {
                    if(result == null){
                        return;
                    }
                    try {
                        JSONObject data = new JSONObject(result);
                        String hrml = data.getString("user_protocol");
                        myWeb.loadDataWithBaseURL(null, hrml, "text/html", "UTF-8", null);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }else {
            OKHttpUtils.getInstance().getRequest("app/user/getPrivacyProtocol", "", new RequestCallback() {
                @Override
                public void onError(int errCode, String errInfo) {
                }
                @Override
                public void onSuccess(String result) {
                    if(result == null){
                        return;
                    }
                    try {
                        JSONObject data = new JSONObject(result);
                        String hrml = data.getString("privacy_protocol");
                        myWeb.loadDataWithBaseURL(null, hrml, "text/html", "UTF-8", null);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
